package com.example.reactor;


import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public class ConditionalExample {
    public static void main(String[] args) {
        // 示例 1: 使用 Mono 条件操作符
        Mono<String> mono = Mono.just("Hello");
        Mono<Boolean> hasElement = mono.hasElement();
        hasElement.subscribe(System.out::println); // 输出: true

        Mono<String> emptyMono = Mono.empty();
        Mono<String> switchIfEmptyMono = emptyMono.switchIfEmpty(Mono.just("Default"));
        switchIfEmptyMono.subscribe(System.out::println); // 输出: Default

        Mono<String> defaultIfEmptyMono = emptyMono.defaultIfEmpty("Default");
        defaultIfEmptyMono.subscribe(System.out::println); // 输出: Default

        // 示例 2: 使用 Flux 条件操作符
        Flux<Integer> flux = Flux.just(1, 2, 3, 4);
        Mono<Boolean> allMatch = flux.all(i -> i > 0);
        allMatch.subscribe(System.out::println); // 输出: true

        Mono<Boolean> anyMatch = flux.any(i -> i > 3);
        anyMatch.subscribe(System.out::println); // 输出: true

        Mono<Boolean> hasElements = flux.hasElements();
        hasElements.subscribe(System.out::println); // 输出: true

        Flux<Integer> emptyFlux = Flux.empty();
        Flux<Integer> switchIfEmptyFlux = emptyFlux.switchIfEmpty(Flux.just(10, 20, 30));
        switchIfEmptyFlux.subscribe(System.out::println); // 输出: 10 20 30

        Flux<Integer> defaultIfEmptyFlux = emptyFlux.defaultIfEmpty(999);
        defaultIfEmptyFlux.subscribe(System.out::println); // 输出: 999
    }
}
